package zhupf.test.juc;

import java.util.concurrent.CountDownLatch;

/**
 * 需求，一共七个线程，主线程必须等到其他六个线程执行完才能执行继续
 */
public class CountDownLatchDemo {
    public static void main(String[] args) {
        //线程计数器
        CountDownLatch downLatch = new CountDownLatch(6);
        for(int i = 0;i<6;i++){
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t线程在执行");
                //调用countDown方法的线程不会阻塞，计数器会减一
                downLatch.countDown();
            },String.valueOf(i)).start();
        }
        try {
            //调用await方法的线程会阻塞，等到程序计数器的值为0时，此线程会被唤醒
            downLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main 线程执行方法");
    }
}
